(function(angular) {
	"use strict";
	angular.module("fs.waf").controller("cls.directCharge.logController", logController);
	logController.$inject = [ "$scope", "$q", "$timeout", "modalService", "gridService", "layoutConfig", "validatorConfig", "authService", "dateService", "directChargeService", "arithService" ];
	function logController($scope, $q, $timeout, modalService, gridService, layoutConfig, validatorConfig, authService, dateService, directChargeService, arithService) {
		var ctrl = this, errorMessage = {};
		$scope.authorization = $scope.$stateParams.authorization;
		ctrl.validatorConfig = validatorConfig;

		ctrl.init = function() {
			if (ctrl.validatorConfig)
				layoutConfig.formConfigs.items.tableConfigs.table.validatorConfig = ctrl.validatorConfig.chargeItems;
			ctrl.chargeItemsGridOptions = gridService.createInnerGridOptions($scope, layoutConfig.formConfigs.items.tableConfigs.table, function(eventRow, selectedRows) {
				ctrl.selectedChargeItems = selectedRows;
			});
			ctrl.chargeItemsGridOptions.data = "vm.directCharge.chargeItems";
			ctrl.chargeItemsGridOptions.addRow = function(row) {
				row._feeCodeConfig = {
					model : "feeCode",
					type : "select",
					filter : "encode",
					options : {
						"callback" : {
							"changed" : "vm.feeCodeChanged"
						}
					},
					size : 12,
					uppercase : true
				};
			};
			directChargeService.initDataOptions().then(function(map) {
				return $q(function(resolved, rejected) {
					var directCharge = {};
					ctrl.prodTypeOptions = map.productType;
					ctrl.countryOptions = map.country;
					ctrl.refCcyOptions = map.refCcy;
					ctrl.allocCodeOptions = map.allocCode;
					ctrl.communityIdOptions = map.communityId;
					directCharge.crFeeInfo = map.crFeeInfo;
					directCharge.drFeeInfo = map.drFeeInfo;
					resolved(directCharge);
				});
			}).then(function(directCharge) {
				authService.logInit().then(function(response) {
					directCharge = angular.extend(directCharge, response.data);
					directCharge.chargeItems = directCharge.chargeItems || [];
					ctrl.directCharge = directCharge;
					ctrl.layoutConfig = layoutConfig;
//					directChargeService.deleteDynaparm(layoutConfig);
				});
			});
		};

		ctrl.prodTypeChanged = function(directCharge, formCtrl, isInitialized) {
			if (isInitialized) {
				return;
			}
			var directCharge_cache = angular.copy(directCharge);
			directChargeService.prodTypeChanged(directCharge, layoutConfig, "directCharge", "log", false).then(function(layout) {
				ctrl.layoutConfig = layout;
				$timeout(ctrl.directCharge = directCharge_cache);
			});
		};

		ctrl.crDrChanged = function(chargeItem) {
			directChargeService.crDrChanged(chargeItem, ctrl.directCharge);
		};
		ctrl.feeCodeChanged = function(chargeItem) {
			directChargeService.feeCodeChanged(chargeItem, ctrl.directCharge);
		};
		
		ctrl.communityIdChanged = function(directCharge){
			if(directCharge.communityId){
				directChargeService.getApCommunity(directCharge.communityId).then(function(map){
					if(map && map.company){
						directCharge.obligorCode = map.company.companyCode;
						directCharge.obligorName = map.company.companyName;
						directCharge.obligorAddr1 = map.company.companyAddr1;
						directCharge.obligorAddr2 = map.company.companyAddr2;
						directCharge.obligorAddr3 = map.company.companyAddr3;
						directCharge.obligorAddr4 = map.company.companyAddr4;
						directCharge.obligorCity = map.company.companyCity;
						directCharge.obligorZip = map.company.companyZip;
						directCharge.obligorState = map.company.companyState;
						directCharge.obligorCountry = map.company.companyCountry;
					}
				});
			}
		};

		ctrl.save = function() {
			if (!directChargeService.validateDirectItem(ctrl.directCharge.chargeItems)) {
				return;
			}
			var messages = [];
			angular.forEach(errorMessage, function(field) {
				angular.forEach(field, function(rule) {
					messages.push(rule);
				});
			});
			if (messages.length > 0) {
				modalService.openErrorDialogs({
					"messages" : messages
				});
			} else {
				return authService.save(ctrl.directCharge).then(submitSuccessful, submitFailed);
			}
		};

		function submitSuccessful(response) {
			modalService.openAcknowledgeDialogs({
				"messages" : [ "title.dialog.successful.submit" ],
				"btnCallback" : {
					"close" : function() {
						$scope.go("cls.workflow.wfTxn.list");
					}
				},
				"redirections" : [ {
					"name" : "title.infobar.workflow.list",
					"state" : "cls.workflow.wfTxn.list",
					"params" : {}
				} ]
			});
		}

		function submitFailed(response) {
			modalService.openErrorDialogs({
				"messages" : response.error
			});
		}

		ctrl.refTenorChanged = function(param, form, firstLoad) {
			if (firstLoad) {
				return;
			}
			directChargeService.refTenorChanged(ctrl.directCharge);
		};

		ctrl.refEffectiveDateChanged = function(param, form, firstLoad) {
			if (firstLoad) {
				return;
			}
			directChargeService.dateChanged(ctrl.directCharge);
		};

		ctrl.refDueDateChanged = function(param, form, firstLoad) {
			if (firstLoad) {
				return;
			}
			directChargeService.dateChanged(ctrl.directCharge);
		};
	}
})(angular);
